# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ignacio Casal Quinteiro <icq@gnome.org>
# Contributor: Renato Silva <br.renatosilva@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

_realname=glib2
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.56.1
pkgrel=1
url="https://www.gtk.org/"
arch=('any')
pkgdesc="Common C routines used by GTK+ 2.4 and other libs (mingw-w64)"
license=(LGPL2)
# options=('debug' '!strip')
install=glib2-${CARCH}.install
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-gettext"
         "${MINGW_PACKAGE_PREFIX}-pcre"
         "${MINGW_PACKAGE_PREFIX}-libffi"
         "${MINGW_PACKAGE_PREFIX}-pcre"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-python3")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-python3"
             "${MINGW_PACKAGE_PREFIX}-python3-setuptools"
             "${MINGW_PACKAGE_PREFIX}-gtk-doc"
             )
source=("https://download.gnome.org/sources/glib/${pkgver%.*}/glib-${pkgver}.tar.xz"
        0001-Use-CreateFile-on-Win32-to-make-sure-g_unlink-always.patch
        0004-glib-prefer-constructors-over-DllMain.patch
        0017-glib-use-gnu-print-scanf.patch
        0023-print-in-binary-more-for-testing.all.patch
        0028-inode_directory.patch
        pyscript2exe.py)
sha256sums=('40ef3f44f2c651c7a31aedee44259809b6f03d3d20be44545cd7d177221c0b8d'
            'ef81e82e15fb3a71bad770be17fe4fea3f4d9cdee238d6caa39807eeea5da3e3'
            '7155b0cdba60a154901336cd231dc2bfc771dc2abfd7d5a577a79c29d5facbb4'
            'e916e4913632485b314269a5c8888e2eff25bb7034f244bc235c08ba7f28b5ea'
            'd1d35bb7865527422e28635ed373731e800c30ff5732c7bf8c66be37158509fb'
            'f7f06a90156fe0a308412512c359072922f7f0d19dd4bed30d863db18e48940b'
            'f68b24932b3365c4098c04eeaeaf87275ceec29694b3f0597c431bbcf4f913a3')


prepare() {
  cd "${srcdir}/glib-${pkgver}"

  patch -Np1 -i "${srcdir}"/0001-Use-CreateFile-on-Win32-to-make-sure-g_unlink-always.patch
  patch -Np1 -i "${srcdir}"/0004-glib-prefer-constructors-over-DllMain.patch
  patch -Np1 -i "${srcdir}"/0017-glib-use-gnu-print-scanf.patch
  patch -Np1 -i "${srcdir}"/0023-print-in-binary-more-for-testing.all.patch
  patch -Np1 -i "${srcdir}"/0028-inode_directory.patch

  NOCONFIGURE=1 ./autogen.sh
}

build() {
  export MSYS2_ARG_CONV_EXCL="-//OASIS//DTD"
  msg "Build shared version"

  mkdir ${srcdir}/build-${CARCH}-shared && cd ${srcdir}/build-${CARCH}-shared

  mkdir -p docs/reference/gio/html
  mkdir -p docs/reference/glib/html
  mkdir -p docs/reference/gobject/html
  cp -rf ../glib-${pkgver}/docs/reference/gio/html/* docs/reference/gio/html
  cp -rf ../glib-${pkgver}/docs/reference/glib/html/* docs/reference/glib/html
  cp -rf ../glib-${pkgver}/docs/reference/gobject/html/* docs/reference/gobject/html

  ../glib-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --disable-static --enable-shared \
    --disable-libelf \
    --with-python=${MINGW_PREFIX}/bin/python3.exe \
    --with-threads=posix \
    --with-xml-catalog=${MINGW_PREFIX}/etc/xml/catalog
  make

  msg "Build static version"
  mkdir ${srcdir}/build-${CARCH}-static && cd ${srcdir}/build-${CARCH}-static
  ../glib-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --disable-shared --enable-static \
    --disable-libelf \
    --with-python=${MINGW_PREFIX}/bin/python3.exe \
    --with-threads=posix \
    --with-xml-catalog=${MINGW_PREFIX}/etc/xml/catalog
  make
}

package() {
  cd "${srcdir}/build-${CARCH}-static"
  make DESTDIR="${pkgdir}/static" install

  cd "${srcdir}/build-${CARCH}-shared"
  make DESTDIR="${pkgdir}" install

  mv "${pkgdir}/static/${MINGW_PREFIX}/lib/"*.a "${pkgdir}/${MINGW_PREFIX}/lib/"

  rm -rf "${pkgdir}/static"

  rm -f "${pkgdir}${MINGW_PREFIX}"/lib/charset.alias
  rm "${pkgdir}${MINGW_PREFIX}/lib/"*.def

  install -Dm644 "${srcdir}/glib-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"

  local PREFIX_DEPS=$(cygpath -am ${MINGW_PREFIX})
  pushd "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig" > /dev/null
  sed -s "s|${PREFIX_DEPS}|${MINGW_PREFIX}|g" -i gio-2.0.pc
  sed -s "s|${PREFIX_DEPS}|${MINGW_PREFIX}|g" -i glib-2.0.pc
  popd > /dev/null

  for name in glib-mkenums glib-genmarshal gdbus-codegen; do
    ${MINGW_PREFIX}/bin/python3 \
      "${srcdir}/pyscript2exe.py" "${pkgdir}${MINGW_PREFIX}/bin/${name}"
  done
}
